package Thread.blocking_queue;

import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 使用阻塞队列实现一个简单的生产消费模型
 */
public class test {
    public static void main(String[] args) throws InterruptedException {
        BlockingDeque<Integer> blockingDeque=new LinkedBlockingDeque<>();
        Thread customer=new Thread(()->{
            while (true){
                try{
                    int val=blockingDeque.take();
                    System.out.println("消费元素："+val);
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }
            }
        },"消费者");
        Random random=new Random();
        Thread producer=new Thread(()->{
            while (true){
                try {
                    int val= random.nextInt(100);
                    blockingDeque.put(val);
                    System.out.println("生产者元素："+val);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"生产者");
        customer.start();
        producer.start();
    }
}
